var needRefProjectStore = null;
var needRefPremisesStore = null;
var needRefBuildingStore = null;
var needRefUnitStore = null;
var needRefRoomStore = null;

Ext
		.define(
				'component.marketing.needRefund',
				{
					extend : 'Ext.form.Panel',
					initComponent : function() {

						// 项目
						needRefProjectStore = new Ext.data.Store(
								{
									singleton : true,
									autoDestroy : true,
									fields : [ "id", "projectName" ],
									proxy : {
										type : 'ajax',
										url : contextPath
												+ '/estate/project/findProjectList?needAuth=T&resourcesCode=menu_0003_0001',
										reader : {
											type : 'json',
											root : 'root' // 数据
										}
									},
									autoLoad : false
								});
						needRefProjectStore.load({
							callback : function(records, options, success) {
								var count = needRefProjectStore.getCount();
								if (count > 0) {
									var id = needRefProjectStore.getAt(0).get(
											"id");
									Ext.getCmp('needRefProjectStore').setValue(
											id)
								}
								if(Ext.Array.contains(idata,"fin:list:all")){
									var data = {"projectName": "全部", "id": '-1'};
									var rs = Ext.create(data);
									needRefProjectStore.insert(count+1, rs);
								}
							}
						});
						// 组团
						needRefPremisesStore = new Ext.data.Store(
								{
									singleton : true,
									autoDestroy : true,
									fields : [ "id", "groupName" ],
									proxy : {
										type : 'ajax',
										url : contextPath
												+ '/estate/projectPremises/findPremisesList',
										reader : {
											type : 'json',
											root : 'root' // 数据
										}
									},
									autoLoad : false
								});

						needRefPremisesStore
								.on(
										"beforeload",
										function() {
											needRefPremisesStore.removeAll();
											needRefBuildingStore.removeAll();
											needRefUnitStore.removeAll();
											needRefRoomStore.removeAll();
											Ext.getCmp("needRefPremisesStore")
													.setValue("");
											Ext.getCmp("needRefBuildingStore")
													.setValue("")
											Ext.getCmp("needRefUnitStore")
													.setValue("")
											Ext.getCmp("needRefRoomStore")
													.setValue("")
											var projectId = Ext.getCmp(
													"needRefProjectStore")
													.getValue();
											if ("-1" == projectId) {
												projectId = "";
											}
											needRefPremisesStore.proxy.extraParams.projectId = projectId;
										});
						// 楼栋
						needRefBuildingStore = new Ext.data.Store(
								{
									singleton : true,
									autoDestroy : true,
									fields : [ "id", "buildingName" ],
									proxy : {
										type : 'ajax',
										url : contextPath
												+ '/estate/projectBuilding/findBuildingList',
										reader : {
											type : 'json',
											root : 'root' // 数据
										}
									},
									autoLoad : false
								});
						needRefBuildingStore
								.on(
										"beforeload",
										function() {
											needRefBuildingStore.removeAll();
											needRefUnitStore.removeAll();
											needRefRoomStore.removeAll();
											Ext.getCmp("needRefBuildingStore")
													.setValue("")
											Ext.getCmp("needRefUnitStore")
													.setValue("")
											Ext.getCmp("needRefRoomStore")
													.setValue("")
											var projectId = Ext.getCmp(
													"needRefProjectStore")
													.getValue();
											if ("-1" == projectId) {
												projectId = "";
											}
											var premisesId = Ext.getCmp(
													"needRefPremisesStore")
													.getValue();
											needRefBuildingStore.proxy.extraParams.projectId = projectId;
											needRefBuildingStore.proxy.extraParams.premisesId = premisesId;
										});
						// 单元
						needRefUnitStore = new Ext.data.Store({
							singleton : true,
							autoDestroy : true,
							fields : [ "id", "unitName" ],
							proxy : {
								type : 'ajax',
								url : contextPath
										+ '/estate/projectUnit/findUnitList',
								reader : {
									type : 'json',
									root : 'root' // 数据
								}
							},
							autoLoad : false
						});
						needRefUnitStore
								.on(
										"beforeload",
										function() {
											needRefUnitStore.removeAll();
											needRefRoomStore.removeAll();
											Ext.getCmp("needRefUnitStore")
													.setValue("")
											Ext.getCmp("needRefRoomStore")
													.setValue("")
											var projectId = Ext.getCmp(
													"needRefProjectStore")
													.getValue();
											if ("-1" == projectId) {
												projectId = "";
											}
											var premisesId = Ext.getCmp(
													"needRefPremisesStore")
													.getValue();
											var buildingId = Ext.getCmp(
													"needRefBuildingStore")
													.getValue();
											needRefUnitStore.proxy.extraParams.projectId = projectId;
											needRefUnitStore.proxy.extraParams.premisesId = premisesId;
											needRefUnitStore.proxy.extraParams.buildingId = buildingId;
										});
						// 房间
						needRefRoomStore = new Ext.data.Store({
							singleton : true,
							autoDestroy : true,
							fields : [ "id", "roomName" ],
							proxy : {
								type : 'ajax',
								url : contextPath
										+ '/estate/projectRoom/findRoomList',
								reader : {
									type : 'json',
									root : 'root' // 数据
								}
							},
							autoLoad : false
						});
						needRefRoomStore
								.on(
										"beforeload",
										function() {
											needRefRoomStore.removeAll();
											Ext.getCmp("needRefRoomStore")
													.setValue("")
											var projectId = Ext.getCmp(
													"needRefProjectStore")
													.getValue();
											if ("-1" == projectId) {
												projectId = "";
											}
											var premisesId = Ext.getCmp(
													"needRefPremisesStore")
													.getValue();
											var buildingId = Ext.getCmp(
													"needRefBuildingStore")
													.getValue();
											var unitId = Ext.getCmp(
													"needRefUnitStore")
													.getValue();
											needRefRoomStore.proxy.extraParams.projectId = projectId;
											needRefRoomStore.proxy.extraParams.premisesId = premisesId;
											needRefRoomStore.proxy.extraParams.buildingId = buildingId;
											needRefRoomStore.proxy.extraParams.unitId = unitId;
										});

						// 房款类型
						var typeRefund = [ {
							"value" : "",
							"text" : "请选择"
						}, {
							"value" : "2",
							"text" : "退认筹金"
						}, {
							"value" : "4",
							"text" : "退认购金"
						}, {
							"value" : "21",
							"text" : "退签约金"
						}, {
							"value" : "23",
							"text" : "补差款"
						} ];
						if (payDepositOff) {
							typeRefund = typeRefund.del(1);
						}
						var relationTypeRefund = Ext.data.Store({
							autoDestroy : true,
							fields : [ "value", "text" ],
							data : typeRefund
						});

						// 房号list
						var roomList = new Ext.data.Store(
								{
									singleton : true,
									autoDestroy : true,
									fields : [ "combinedName", "id" ],
									proxy : {
										type : 'ajax',
										url : jypath
												+ '/marketing/haveReceiveRefund/findRoomId',
										reader : {
											type : 'json',
											root : 'root' // 数据
										}
									},
									autoLoad : true
								});

						// 员工list
						var needRefUserList = new Ext.data.Store(
								{
									fields : [ "userName", "id" ],
									proxy : {
										type : 'ajax',
										url : jypath
												+ '/customer/customer/userByProjectIdForGroup',
										reader : {
											type : 'json',
											root : 'root' // 数据
										}
									},
									autoLoad : false
								});

						needRefUserList
								.on(
										"beforeload",
										function() {
											var projectId = Ext.getCmp(
													"needRefProjectStore")
													.getValue();
											if ("-1" == projectId) {
												projectId = "";
												return false;
											}
											needRefUserList.proxy.extraParams.projectId = projectId;
										})

						var formRefund = Ext
								.create(
										"Ext.form.Panel",
										{
											// minHeight:auto,
											title : "查询条件",
											collapsible : true, // 可折叠
											autoScroll : false, // 自动创建滚动条
											defaultType : 'textfield',
											defaults : {
												anchor : '100%'
											},
											fieldDefaults : {
												margin : 2,
												labelWidth : 80,
												labelAlign : "right",
												margin : "5 0 0 0"
											},
											items : [
													{
														xtype : "hiddenfield",
														name : "docType",
														value : "1"
													},
													{
														xtype : "container",
														layout : "hbox",
														items : [
																{
																	xtype : 'combobox',
																	id : 'needRefProjectStore',
																	name : "projectId",
																	store : needRefProjectStore,
																	valueField : "id",
																	displayField : "projectName",
																	fieldLabel : '所属项目',
																	queryMode : 'local',
																	typeAhead : true,
																	listeners : {
																		'change' : function(
																				value) {
																			// 加载组团数据
																			if (value.lastValue
																					&& value.lastValue != "-1") {
																				needRefUserList
																						.load();
																				needRefPremisesStore
																						.load({
																							callback : function(
																									records,
																									operation,
																									success) {
																								// 若无组团数据，加载楼栋
																								var recordLen = records.length;
																								if (recordLen == 0) {
																									needRefBuildingStore
																											.load();
																								}
																							}
																						});
																			} else {
																				needRefUserList
																						.removeAll();
																				needRefPremisesStore
																						.removeAll();
																				needRefBuildingStore
																						.removeAll();
																				needRefUnitStore
																						.removeAll();
																				needRefRoomStore
																						.removeAll();
																				Ext
																						.getCmp(
																								"needRefPremisesStore")
																						.setValue(
																								"");
																				Ext
																						.getCmp(
																								"needRefBuildingStore")
																						.setValue(
																								"")
																				Ext
																						.getCmp(
																								"needRefUnitStore")
																						.setValue(
																								"")
																				Ext
																						.getCmp(
																								"needRefRoomStore")
																						.setValue(
																								"")
																			}
																		}
																	}
																},
																{
																	xtype : 'combobox',
																	id : 'needRefPremisesStore',
																	name : "premisesId",
																	store : needRefPremisesStore,
																	valueField : "id",
																	displayField : "groupName",
																	queryMode : 'local',
																	typeAhead : true,
																	listeners : {
																		'change' : function(
																				value) {
																			// 加载楼栋
																			// 若无楼栋数据，加载单元
																			if (value.lastValue
																					&& value.lastValue != "-1") {
																				needRefBuildingStore
																						.load({
																							callback : function(
																									records,
																									operation,
																									success) {
																								// 若无组团数据，加载楼栋
																								var recordLen = records.length;
																								if (recordLen == 0) {
																									needRefUnitStore
																											.load();
																								}
																							}
																						});
																			}
																		}
																	}
																},
																{
																	xtype : 'combobox',
																	id : 'needRefBuildingStore',
																	name : "buildingId",
																	store : needRefBuildingStore,
																	valueField : "id",
																	displayField : "buildingName",
																	queryMode : 'local',
																	typeAhead : true,
																	listeners : {
																		'change' : function(
																				value) {
																			// 加载单元
																			// 若无单元数据，加载房间
																			if (value.lastValue
																					&& value.lastValue != "-1") {
																				needRefUnitStore
																						.load({
																							callback : function(
																									records,
																									operation,
																									success) {
																								// 若无组团数据，加载楼栋
																								var recordLen = records.length;
																								if (recordLen == 0) {
																									needRefRoomStore
																											.load();
																								}
																							}
																						});
																			}
																		}
																	}
																},
																{
																	xtype : 'combobox',
																	id : 'needRefUnitStore',
																	name : "unitId",
																	store : needRefUnitStore,
																	valueField : "id",
																	displayField : "unitName",
																	queryMode : 'local',
																	typeAhead : true,
																	listeners : {
																		'change' : function(
																				value) {
																			// 加载房间
																			if (value.lastValue
																					&& value.lastValue != "-1") {
																				needRefRoomStore
																						.load();
																			}
																		}
																	}
																},
																{
																	xtype : 'combobox',
																	id : 'needRefRoomStore',
																	name : "roomId",
																	store : needRefRoomStore,
																	valueField : "id",
																	displayField : "roomName",
																	queryMode : 'local',
																	typeAhead : true
																} ]
													},
													{
														xtype : "container",
														layout : "hbox",
														items : [ {
															xtype : "checkboxgroup",
															id : "busStatusRefund",
															fieldLabel : "业务类型",
															name : "businessType",
															width : 400,
															items : [
																	{
																		boxLabel : '认筹',
																		inputValue : '0',
																		hidden : payDepositOff
																	},
																	{
																		boxLabel : '认购',
																		inputValue : '1'
																	},
																	{
																		boxLabel : '签约',
																		inputValue : '2'
																	},
																	{
																		boxLabel : '面积补差',
																		inputValue : '3'
																	} ]
														} ]
													},
													{
														xtype : "container",
														layout : "hbox",
														items : [
																{
																	xtype : 'combobox',
																	fieldLabel : '房款类型',
																	store : relationTypeRefund,
																	displayField : "text",
																	valueField : "value",
																	id : 'relationTypeN',
																	name : 'housingFundType'
																},
																{
																	xtype : 'combobox',
																	fieldLabel : '经&nbsp;&nbsp;纪&nbsp;人',
																	store : needRefUserList,
																	displayField : "userName",
																	valueField : "id",
																	name : 'createUserId'
																} ]
													},
													{
														xtype : "container",
														layout : "hbox",
														items : [
																{
																	xtype : "textfield",
																	name : "relationCode",
																	fieldLabel : "业务编号"
																},
																{
																	xtype : "textfield",
																	name : "customer_name",
																	fieldLabel : "客户名称"
																}

														]
													},
													{
														xtype : "container",
														layout : "hbox",
														items : [ {
															xtype : 'container',
															layout : 'hbox',
															items : [
																	{
																		xtype : 'datefield',
																		fieldLabel : '开始有效日期',
																		name : 'ysDateStart',
																		format : 'Y-m-d'
																	},
																	{
																		xtype : 'datefield',
																		name : 'ysDateEnd',
																		format : 'Y-m-d',
																		fieldLabel : '结束有效日期'
																	} ]
														} ]
													} ],
											buttons : [
													{
														xtype : "button",
														text : "查询",
														handler : function() {
															// 获取通过checkboxgroup定义的checkbox值
															// 业务状态
															var busStatusValue = Ext
																	.getCmp(
																			'busStatusRefund')
																	.getChecked();
															var busStatus = "";
															Ext.Array
																	.each(
																			busStatusValue,
																			function(
																					item) {
																				busStatus += item.inputValue
																						+ ',';
																			});
															gridStoreRefund
																	.getProxy().extraParams.businessType = busStatus;
															gridStoreRefund
																	.load();
															var pagingTool = Ext
																	.getCmp("pagingTool");
															pagingTool
																	.moveFirst();// 跳至第一页
														}
													},
													{
														xtype : "button",
														text : "导出",
														hidden : finNeedReceiveExport,
														handler : function() {
															var count = gridRefund
																	.getStore()
																	.getCount();
															if (count <= 0) {
																Ext.MessageBox
																		.alert(
																				"系统提示",
																				"没有可导出的数据");
															} else {
																var busStatusValue = Ext
																		.getCmp(
																				'busStatusRefund')
																		.getChecked();
																var busStatus = "";
																Ext.Array
																		.each(
																				busStatusValue,
																				function(
																						item) {
																					busStatus += item.inputValue
																							+ ',';
																				});
																gridStoreRefund
																		.getProxy().extraParams.businessType = busStatus;
																var value = encodeURI(encodeURI(
																		JSON
																				.stringify(gridStoreRefund
																						.getProxy().extraParams),
																		"utf-8"));
																var url = jypath
																		+ "/marketing/needReceiveRefund/needReceicerefundExport?json="
																		+ value;
																location.href = url;
															}
														}
													},
													{
														xtype : "button",
														text : "清空",
														handler : function() {
															formRefund
																	.getForm()
																	.reset();
														}
													} ]
										});

						var gridStoreRefund = Ext
								.create(
										'Ext.data.Store',
										{
											fields : [ "relationId",
													"businessType",
													"relationCode",
													"customer_name",
													"combined_name", "price",
													"calculatArea",
													"relationType",
													"uncollectedAmount",
													"createTime",
													"statement_name", "remark" ],
											pageSize : 10, // 页容量15条数据
											// 是否在服务端排序 （true的话，在客户端就不能排序）
											remoteSort : false,
											proxy : {
												type : 'ajax',
												url : jypath
														+ '/marketing/needReceiveRefund/list?resourcesCode=menu_0003_0001',
												actionMethods : {
													read : 'POST'
												},
												reader : { // 这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
													type : 'json', //返回数据类型为json格式
													root : 'rows', //数据
													totalProperty : 'total' //数据总条数
												}
											},
											autoLoad : true
										//即时加载数据
										});

						gridStoreRefund
								.on(
										"beforeload",
										function() {
											Ext
													.apply(
															gridStoreRefund.proxy.extraParams,
															formRefund
																	.getForm()
																	.getValues());
											var projectId = Ext.getCmp(
													"needRefProjectStore")
													.getValue();
											if (projectId == "-1") {
												gridStoreRefund.proxy.extraParams.projectId = "";
											}
											var newResCode = "menu_0003_0001";
											gridStoreRefund.proxy.extraParams.resourcesCode = newResCode;
										});

						function onreload() {
							gridStoreRefund.on("beforeload", function() {
								Ext.apply(gridStoreRefund.proxy.extraParams,
										formRefund.getForm().getValues());
							});
						}

						var gridRefund = Ext
								.create(
										'Ext.grid.Panel',
										{
											minHeight : 450,
											minWidth : 300,
											store : gridStoreRefund,
											columnLines : true,
											columns : [
													{
														text : "操作",
														locked : true,
														dataIndex : 'actioncolumn',
														renderer : function(
																value,
																cellmeta,
																record,
																rowIndex,
																columnIndex,
																store) {
															var rec = gridRefund
																	.getStore()
																	.getAt(
																			rowIndex);
															//退款
															var html = "";
															if (finHaveReceiveRefund == false) {
																html = '<a onclick="refund_need('
																		+ rec
																				.get('id')
																		+ ')" style="cursor:pointer">退款</a>';
															}

															return html;
														}
													},
													{
														text : '业务编号',
														dataIndex : 'relation',
														renderer : function(
																value,
																cellmeta,
																record,
																rowIndex,
																columnIndex,
																store) {

															var rec = gridRefund
																	.getStore()
																	.getAt(
																			rowIndex);
															var url = "";
															if (rec
																	.get('businessType') == "0") {
																//认筹/marketing/needReceiveRefund/index
																url = jypath
																		+ "/customer/payDeposit/toView?id="
																		+ rec
																				.get('relationId')
																		+ '&returnUrl=,marketing,needReceiveRefund,index';
															} else if (rec
																	.get('businessType') == "1") {
																//认购
																url = jypath
																		+ "/customer/subscribe/toView?id="
																		+ rec
																				.get('relationId')
																		+ '&returnUrl=,marketing,needReceiveRefund,index';
															} else if (rec
																	.get('businessType') == "2") {
																//签约
																url = jypath
																		+ "/customer/signed/toView?id="
																		+ rec
																				.get('relationId')
																		+ '&returnUrl=,marketing,needReceiveRefund,index';
															} else if (rec
																	.get('businessType') == "3") {
																//面积补差
																url = jypath
																		+ "/marketing/reserveArea/reserveAreaDetail?id="
																		+ rec
																				.get('relationId');
															}
															var html = '<a href="'
																	+ url
																	+ '" style="cursor:pointer">'
																	+ rec
																			.get('relationCode')
																	+ '</a>';
															return html;
														}
													},
													{
														text : '客户名称',
														dataIndex : 'customer_name'
													},
													{
														text : '房号',
														dataIndex : 'combined_name',
														width : 350
													},
													{
														text : '签约面积(㎡)',
														dataIndex : 'calculatArea'
													},
													{
														text : '签约单价(元)',
														dataIndex : 'price'
													},
													{
														text : '退款类型',
														dataIndex : 'relationType'
													},
													{
														text : '可退金额(元)',
														dataIndex : 'uncollectedAmount'
													},
													{
														text : '经办日期',
														dataIndex : 'createTime',
														renderer : function(
																value) {
															if (value != ""
																	&& value != null
																	&& value != "null") {
																return Ext.Date
																		.format(
																				new Date(
																						value),
																				'Y-m-d');
															} else {
																return "";
															}
														}
													},
													{
														text : '经办人',
														dataIndex : 'statement_name'
													}, {
														text : '备注',
														dataIndex : 'remark'
													} ],
											title : '待退款明细列表',
											dockedItems : [ {
												xtype : 'pagingtoolbar',
												store : gridStoreRefund, // GridPanel中使用的数据  
												dock : 'bottom',
												displayInfo : true,
												emptyMsg : "没有数据",
												displayMsg : "显示从{0}条数据到{1}条数据，共{2}条数据",
												id : 'pagingTool'
											} ]
										});

						var defaultConfigRefund = {
							defaults : {
								split : true, //有分割线  
								collapsible : true
							//可以折叠
							},
							items : [ formRefund, gridRefund ]
						};
						Ext.apply(this, defaultConfigRefund);
						this.callParent(arguments);

					}
				});

function refund_need(id) {
	location.href = jypath + "/marketing/needReceiveRefund/refund_view?id="
			+ id;
}
